package com.example.nbshangpping.goods;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nbshangpping.R;
import com.example.nbshangpping.buy.BuyActivity;
import com.example.nbshangpping.login.UserBean;
import com.example.nbshangpping.utils.BaseActivity;
import com.example.nbshangpping.utils.BuyNumDialog;
import com.example.nbshangpping.utils.SaveUserUtils;
import com.example.nbshangpping.utils.URLUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DetailsGoodsActivity extends BaseActivity {
    TextView titletv,pricetv,numtv,destv;
    ImageView backiv,showiv;
    Button buybtn,cartbtn;
    GridView likegv;
    GoodsBean.DataBean dataBean;
    List<GoodsBean.DataBean> data;
    private LikeAdapter adapter;
    private UserBean.DataBean userInfo;
    boolean islogin = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_goods);
        islogin = islogin();
        initView();
        setListener();
        dataBean = (GoodsBean.DataBean)
                getIntent().getSerializableExtra("goods");
        setViewContent();
        //设置猜你喜欢的view
        data = new ArrayList<>();
        //适配器
        adapter = new LikeAdapter(this, data);
        likegv.setAdapter(adapter);
        //联网
        getNetwork(URLUtils.guessYouLike_url);
    }
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        GoodsBean goodsBean = new Gson().fromJson(result, GoodsBean.class);
        if (goodsBean.getCode()==200) {
            if (goodsBean.getData()==null) {
                Toast.makeText(this,"添加购物车成功",Toast.LENGTH_SHORT).show();
            }else {
                List<GoodsBean.DataBean> list = goodsBean.getData();
                data.addAll(list);
                adapter.notifyDataSetChanged();
            }
        }
    }

    //  设置view显示的内容
    private void setViewContent() {
        titletv.setText(dataBean.getName());
        pricetv.setText("¥ "+dataBean.getPrice()+"元");
        numtv.setText("剩余 "+dataBean.getCount()+"件");
        destv.setText(dataBean.getInfo());
        String photo = dataBean.getPhoto();
        if (!TextUtils.isEmpty(photo)) {
            String photourl = URLUtils.PUBLIC_URL+photo;
            Picasso.with(this).load(photourl).into(showiv);
        }
    }

    // 设置监听工作的
    private void setListener() {
        backiv.setOnClickListener(clickListener);
        buybtn.setOnClickListener(clickListener);
        cartbtn.setOnClickListener(clickListener);
        likegv.setOnItemClickListener(itemClickListener);
    }
    //     gridview的监听对象，负责给每一项设置监听
    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            GoodsBean.DataBean dataBean = data.get(position);
            Intent intent = new Intent(DetailsGoodsActivity.this, DetailsGoodsActivity.class);
            intent.putExtra("goods",dataBean);  //将数据传递给下一级页面
            startActivity(intent);
//            finish();
        }
    };
    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.details_btn_buy:
                    String msg = "购买之前请先登录";
                    showBuyDialog(msg,1);
                    break;
                case R.id.details_btn_cart:
                    msg = "添加购物车之前请先登录";
                    showBuyDialog(msg,2);
                    break;
                case R.id.details_iv_back:
                    finish();   //结束当前的页面
                    break;
            }
        }
    };
    // 显示购买数量对话框的函数
    private void showBuyDialog(String msg,int flag) {
        if (islogin) {  //登录
            BuyNumDialog dialog = new BuyNumDialog(DetailsGoodsActivity.this);
            dialog.show();
            dialog.setStorage_num(dataBean.getCount()); //设置库存
            dialog.setDialogWidth();
            dialog.setOnEnsureListener(new BuyNumDialog.OnEnsureListener() {
                @Override
                public void onEnsure(int num) {
                    if (flag == 1) {   //购买按钮点击弹出的
                        //小组自己完成，跳转到支付页面，传的数据，传商品id和购买数量num
                        Intent intent=new Intent(DetailsGoodsActivity.this, BuyActivity.class);
//                        intent.putExtra("price",dataBean.getPrice()+"");
//                        intent.putExtra("num",num+"");
//                        intent.putExtra("id",dataBean.getId()+"");
                        intent.putExtra("selectdata",dataBean);
                        intent.putExtra("num",num+"");
                        startActivity(intent);
                    }else if (flag == 2) {  //购物车按钮点击弹出来
                        Map<String,String>map = new HashMap<>();
                        map.put("userId",userInfo.getId()+"");
                        map.put("commodityId",dataBean.getId()+"");
                        map.put("count",num+"");
                        postNetwork(URLUtils.insertShoppingCar_url,map);
                    }
                    dialog.cancel();
                }
            });
        }else {  //未登录
            Toast.makeText(DetailsGoodsActivity.this,
                    msg,Toast.LENGTH_SHORT).show();
        }
    }

    //判断是否登录
    private boolean islogin() {
        userInfo = SaveUserUtils.getUserInfo(this);  //表示登录的用户信息
        if (userInfo == null) {
            return false;
        }
        return true;
    }

    private void initView() {
        titletv = findViewById(R.id.details_tv_name);
        pricetv = findViewById(R.id.details_tv_price);
        numtv = findViewById(R.id.details_tv_num);
        destv = findViewById(R.id.details_tv_des);
        backiv = findViewById(R.id.details_iv_back);
        showiv = findViewById(R.id.details_iv_goods);
        buybtn = findViewById(R.id.details_btn_buy);
        cartbtn = findViewById(R.id.details_btn_cart);
        likegv = findViewById(R.id.details_gv);
    }
}
